package com.ghsc.ghxc.jsb.ui.main

import androidx.lifecycle.MutableLiveData
import com.cn.appcore.base.BaseViewModel
import com.cn.appcore.ext.executeResponse
import com.cn.appcore.http.bean.BaseResponse
import com.ghsc.ghxc.jsb.api.ApiMarket
import com.ghsc.ghxc.jsb.ui.bean.SysConfigResult
import com.ghsc.ghxc.jsb.ui.main.bean.MainNotifyResult
import com.ghsc.ghxc.jsb.ui.main.bean.ToTkenResult
import com.ghsc.ghxc.jsb.ui.my.bean.UserResultDataBean
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext


class MainViewModel : BaseViewModel() {

    /**  配置 **/
    val onSysConfigData by lazy { MutableLiveData<SysConfigResult>() }
    fun onSysConfigMethod(paras: String) {
        launch {
            val response = withContext(Dispatchers.IO) { ApiMarket.service.onSysConfigData(paras) }
            executeResponse(
                response,
                { onSysConfigData.value = response.data },
                {
                    againLoginMsg.value = response.msg
                },
            ) { errorMsg.value = response.msg }
        }
    }

    /**  公告 **/
    val onNotifyData: MutableLiveData<BaseResponse<MainNotifyResult>> = MutableLiveData()
    fun onNotifyMethod(paras: String) {
        launch {
            val response = withContext(Dispatchers.IO) { ApiMarket.service.onNotifyData(paras) }
            executeResponse(
                response,
                { onNotifyData.value = response },
                { againLoginMsg.value = response.msg }
            ) { errorMsg.value = response.msg }
        }
    }


    /**
     * 个人中心
     */
    val onUserInfoData: MutableLiveData<BaseResponse<UserResultDataBean>> = MutableLiveData()
    fun onUserInfoMethod() {
        launch {
            val response = withContext(Dispatchers.IO) { ApiMarket.service.onUserInfoData() }
            executeResponse(
                response,
                { onUserInfoData.value = response },
                { againLoginMsg.value = response.msg }
            ) { errorMsg.value = response.msg }
        }

    }
    /**  云信 token **/
    val onGetTokenData: MutableLiveData<BaseResponse<ToTkenResult>> = MutableLiveData()
    fun onGetTokenMethod(p:String) {
        launch {
            val response = withContext(Dispatchers.IO) { ApiMarket.service.onGetTokenData(p) }
            executeResponse(
                response,
                { onGetTokenData.value = response },
                { againLoginMsg.value = response.msg }
            ) { errorMsg.value = response.msg }
        }

    }

    /**
     * 云信 系统通知
     */
    val onYxMessageConvertData: MutableLiveData<BaseResponse<Any>> = MutableLiveData()
    fun onYxMessageConvertMethod(json: String) {
        launch {
            val response =
                withContext(Dispatchers.IO) { ApiMarket.service.onYxMessageConvertData(json) }
            executeResponse(
                response,
                { onYxMessageConvertData.value = response },
                { againLoginMsg.value = response.msg }
            ) { errorMsg.value = response.msg }
        }

    }
}